package com.kaesar.leetcode.LeetCode_051_100;

public class LeetCode_091 {

    private static int result = 0;

    /**
     * 递归 穷举：性能较差，提交会超时
     *
     * @param s
     * @return
     */
    public static int numDecodings(String s) {
        // 这些情况无法映射，直接返回0
        if (s == null || s == "" || s.equals("0") || s.startsWith("0")) {
            return 0;
        }

        if (s.length() == 1) {
            return 1;
        }

        numDecodings(s, 0, 1);
        numDecodings(s, 0, 2);
        return result;
    }



    public static void numDecodings(String s, int left, int right) {
        if (s.charAt(left) == '0') {
            return;
        }
        if (Integer.valueOf(s.substring(left, right)) > 26) {
            return;
        }
        if (s.length() - right == 0) {
            result++;
            return;
        }
        if (s.length() - right == 1 && s.charAt(s.length() - 1) != '0') {
            result++;
            return;
        }
        numDecodings(s, right, right + 1);
        if (s.length() - right > 1) {
            numDecodings(s, right, right + 2);
        }
    }

    public static void main(String[] args) {
        System.out.println(numDecodings("226"));
    }
}
